//
//  HLRetweetedStatuseView.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/3/5.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

import SnapKit
///被转发微博
class HLRetweetedStatuseView: UIView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var bottonContraint:Constraint?
    
    var reweetedStatuse:HLStatuses? {
        didSet {
            
            // 如果是转发微博就赋值
            retweetedLable.text = "@\(reweetedStatuse?.user?.screen_name)\(reweetedStatuse?.text)"
            bottonContraint?.uninstall()
            if let pictures = reweetedStatuse?.storePictureUrl where pictures.count > 0 {
                
                retweetedPicture.hidden = false
                retweetedPicture.statuses = reweetedStatuse
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottonContraint = make.bottom.equalTo(retweetedPicture).constraint
                })
            }else  {
                retweetedPicture.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottonContraint = make.bottom.equalTo(retweetedLable).constraint
                })
            }
        }
    }
    
    init() {
        super.init(frame: CGRectZero)
        
        prepareUI()
    }
    
    /// 设置UI相关操作
    func prepareUI() {
        addSubview(retweetedBackButton)
        addSubview(retweetedLable)
        addSubview(retweetedPicture)
        
        retweetedBackButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        retweetedLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(speaceMagin)
            make.trailing.equalTo(self).offset(-speaceMagin)
            make.top.equalTo(self).offset(speaceMagin)
        }
        
        retweetedPicture.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLable.snp_bottom).offset(speaceMagin)
            make.leading.equalTo(self).offset(speaceMagin)
            
            // 缺少size的约束 自身的内部约束了 
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            bottonContraint = make.bottom.equalTo(retweetedPicture).constraint
        }
        
    }
    
    
    
    /// 懒加载子控件
    
    lazy var retweetedBackButton:UIButton = UIButton(title: "", backeImageName: "retweet_background", font: 0, imageName: "")
    
    lazy var retweetedLable:UILabel = {
        let lable = UILabel()
        
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(15)
        lable.numberOfLines = 0

        return lable
    }()
    
    lazy var retweetedPicture:HLPickturesView = HLPickturesView()
}
